﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static WinFormsApp1.TongShiWei.segmentationData.SegmentationContext;
using WinFormsApp1.Common;
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json.Bson;
using Microsoft.VisualBasic.Devices;

namespace WinFormsApp1.TongShiWei.segmentationData
{
    public partial class UpSegmentationData : UIForm
    {
        int RKWeighDataId_ = 0;
        public UpSegmentationData(int RKWeighDataId)
        {
            InitializeComponent();
            RKWeighDataId_ = RKWeighDataId;
            ShowRKWeightData();
            //ShowUPCData();
        }
        //反填数据
        public async void ShowRKWeightData()
        {
            var res = await HttpClientHelper.RequestUrl<RKWeighData_Now, ApiResult<List<RKWeighData_Now>>>(URLCommon.Url2, "/TSW/ShowRKWeighData", HttpClientHelper.MethodType.GET);
            if (res.Code > 0)
            {
                var GetRKWeightData = res.Data.FirstOrDefault(x => x.RKWeighId == RKWeighDataId_);
                if (GetRKWeightData != null)
                {
                    this.uiTextBox1.Text = GetRKWeightData.UPCName;
                    this.uiTextBox6.Text = GetRKWeightData.GrossNum.ToString();
                    this.uiTextBox5.Text = GetRKWeightData.furNum.ToString();
                    this.uiTextBox4.Text = GetRKWeightData.suttleNum.ToString();
                    this.uiTextBox3.Text = GetRKWeightData.WeighNum.ToString();
                    this.uiDatetimePicker1.Text = GetRKWeightData.WeighTime.ToString();
                }
            }
        }
        #region 显示商品代号
        //显示分割称重入库表数据
        public class RKWeighData_Now
        {
            /// <summary>
            /// 分割称重入库ID
            /// </summary>
            public int RKWeighId { get; set; }
            /// <summary>
            /// 称重件数
            /// </summary>
            public int WeighNum { get; set; }
            /// <summary>
            /// 毛重
            /// </summary>
            public double GrossNum { get; set; }
            /// <summary>
            /// 皮重
            /// </summary>
            public double furNum { get; set; }
            /// <summary>
            /// 净重
            /// </summary>
            public double suttleNum { get; set; }
            /// <summary>
            /// 称重时间
            /// </summary>
            public DateTime WeighTime { get; set; }
            /// <summary>
            /// 包装名称
            /// </summary>
            public string PackName { get; set; } = string.Empty;
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsWeigh { get; set; }
            /// <summary>
            /// 总重
            /// </summary>
            public double SumWeigh { get; set; }
            /// <summary>
            /// 入库数量
            /// </summary>
            public int RKWeighNum { get; set; }
            /// <summary>
            /// 商品代号
            /// </summary>
            public string UPCName { get; set; } = string.Empty;
        }
        //显示商品代号表数据
        int ynum = 1;
        int qnum = 20;
        int znum = 0;
        int AddNum = 0;
        List<Button> buttons = new List<Button>();
        public async void ShowUPCData()
        {
            if (Computer_.Count > 0)
            {
                foreach (var item in Computer_)
                {
                    this.Controls.Remove(item);
                }
                Computer_.Clear();
            }
            //http://localhost:5106/api/Tsw/ShowUPCData
            var res = await HttpClientHelper.RequestUrl<UPCData, ApiResult<List<UPCData>>>(URLCommon.Url2, "/TSW/ShowUPCData", HttpClientHelper.MethodType.GET);
            if (res.Code > 0)
            {
                znum = Convert.ToInt32(Math.Ceiling((double)res.Data.Count / qnum));
                var RKWeightListData = res.Data.OrderBy(x => x.GoodsMinuteId).Skip((ynum - 1) * qnum).Take(qnum).ToList();
                int x = 400; int y = 115; int i = 1;
                foreach (var item in RKWeightListData)
                {
                    Button button = new Button()
                    {
                        Location = new Point(x, y),
                        Size = new Size(130, 70),
                        BackColor = Color.Gainsboro,
                        //RadioButtonColor = Color.Transparent,
                        ForeColor = Color.Black,
                        Text = item.UPCDataName,
                        Name = item.GoodsMinuteId.ToString()
                    };
                    button.Click += new System.EventHandler(UpDataUPCData);
                    buttons.Add(button);
                    this.Controls.Add(button);
                    if (i == 4)
                    {
                        i = 1;
                        y += 100;
                        x = 400;
                    }
                    else
                    {
                        x += 140;
                        i++;
                    }
                }
                //500/611   700/611  //150/50
                if (AddNum == 0)
                {
                    Button button = new Button()
                    {
                        Location = new Point(500, 611),
                        Size = new Size(150, 50),
                        BackColor = Color.DarkGray,
                        //RadioButtonColor = Color.Transparent,
                        ForeColor = Color.Black,
                        Text = "上一页",
                        Name = "GoYnumData"
                    };
                    buttons.Add(button);
                    button.Click += new System.EventHandler(uiButton5_Click);
                    this.Controls.Add(button);
                    Button button2 = new Button()
                    {
                        Location = new Point(700, 611),
                        Size = new Size(150, 50),
                        BackColor = Color.DarkGray,
                        //RadioButtonColor = Color.Transparent,
                        ForeColor = Color.Black,
                        Text = "下一页",
                        Name = "NextYnumData",
                    };
                    buttons.Add(button2);
                    button2.Click += new System.EventHandler(uiButton4_Click);
                    this.Controls.Add(button2);
                    AddNum++;
                }
            };
        }
        //点击商品代号给文本框赋值
        public async void UpDataUPCData(object sender, EventArgs e)
        {
            var ButtonData = sender as Button;
            this.uiTextBox1.Text = ButtonData.Text;
        }
        //商品代号表字段
        public class UPCData
        {
            /// <summary>
            /// 商品代号ID
            /// </summary>
            public int GoodsMinuteId { get; set; }
            /// <summary>
            /// 助记码
            /// </summary>
            public string MemoryNum { get; set; } = string.Empty;
            /// <summary>
            /// 商品分类Id
            /// </summary>
            public int CommodityId { get; set; }
            /// <summary>
            /// 顺序号 
            /// </summary>
            public int OrderNum { get; set; }
            /// <summary>
            /// 等级ID
            /// </summary>
            public int GoodsGradeId { get; set; }
            /// <summary>
            /// 商品代号名称
            /// </summary>
            public string UPCDataName { get; set; } = string.Empty;
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsGoodsMinute { get; set; } = false;
        }
        //关闭页面
        private void uiButton6_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //上一页
        private void uiButton5_Click(object sender, EventArgs e)
        {
            if (ynum < 2)
            {
                MessageBox.Show("已经是第一页了");
                return;
            }
            else
            {
                ynum -= 1;
                foreach (var item in buttons)
                {
                    this.Controls.Remove(item);
                }
                buttons.Clear();
                AddNum = 0;
                ShowUPCData();
            }
        }
        //下一页
        private void uiButton4_Click(object sender, EventArgs e)
        {
            if (ynum == znum)
            {
                MessageBox.Show("已经是最后一页了");
                return;
            }
            else
            {
                ynum += 1;
                foreach (var item in buttons)
                {
                    this.Controls.Remove(item);
                }
                buttons.Clear();
                AddNum = 0;
                ShowUPCData();
            }
        }
        #endregion
        #region 文本框点击事件
        //商品代号
        private void uiTextBox1_Click(object sender, EventArgs e)
        {
            AddNum = 0;
            ShowUPCData();
        }
        //毛重
        private void uiTextBox6_Click(object sender, EventArgs e)
        {
            NumType = 1;
            ShowComputer();
        }
        //皮重
        private void uiTextBox5_Click(object sender, EventArgs e)
        {
            NumType = 2;
            ShowComputer();
        }
        //净重
        private void uiTextBox4_Click(object sender, EventArgs e)
        {
            NumType = 3;
            ShowComputer();
        }
        //件数
        private void uiTextBox3_Click(object sender, EventArgs e)
        {
            NumType = 4;
            ShowComputer();
        }
        #endregion
        #region 显示计算机
        List<Button> Computer_ = new List<Button>();
        //显示计算机的控件
        public async void ShowComputer()
        {
            if (buttons.Count > 0)
            {
                foreach (var item in buttons)
                {
                    this.Controls.Remove(item);
                }
                buttons.Clear();
            }
            List<string> ComputerData = new List<string>()
            { "7","8","9","4","5","6","1","2","3","0",".","退格","00","清零"};
            int x = 500; int y = 115; int i = 1; int L = 0;
            foreach (var item in ComputerData)
            {
                Button button = new Button()
                {
                    Location = new Point(x, y),
                    Size = new Size(111, 100),
                    BackColor = Color.Gainsboro,
                    //RadioButtonColor = Color.Transparent,
                    ForeColor = Color.Black,
                    Text = item,
                    Name = item
                };
                L++;
                if (L == 14)
                {
                    button.Size = new Size(261, 100);
                }
                button.Click += new System.EventHandler(UpComputerData);
                Computer_.Add(button);
                this.Controls.Add(button);
                if (i == 3)
                {
                    i = 1;
                    y += 105;
                    x = 500;
                }
                else
                {
                    x += 150;
                    i++;
                }
            }

        }
        //计算机点击事件
        int NumType = 0;
        public async void UpComputerData(object sender, EventArgs e)
        {
            var buttonData = sender as Button;
            switch (NumType)
            {
                case 1:
                    if (buttonData.Text == "退格")
                    {
                        if (this.uiTextBox6.Text.Length > 0)
                        {
                            this.uiTextBox6.Text = this.uiTextBox6.Text.Substring(0, this.uiTextBox6.Text.Length - 1);
                        }
                        else
                        {
                            this.uiTextBox6.Text = "0";
                        }
                    }
                    else if (buttonData.Text == "清零")
                    {
                        this.uiTextBox6.Text = "0";
                    }
                    else
                    {
                        this.uiTextBox6.Text = this.uiTextBox6.Text == "0" ? "" : this.uiTextBox6.Text;
                        this.uiTextBox6.Text += buttonData.Text;
                    }
                    break;
                case 2:
                    if (buttonData.Text == "退格")
                    {
                        if (this.uiTextBox5.Text.Length > 0)
                        {
                            this.uiTextBox5.Text = this.uiTextBox5.Text.Substring(0, this.uiTextBox5.Text.Length - 1);
                        }
                        else
                        {
                            this.uiTextBox5.Text = "0";
                        }
                    }
                    else if (buttonData.Text == "清零")
                    {
                        this.uiTextBox5.Text = "0";
                    }
                    else
                    {
                        this.uiTextBox5.Text = this.uiTextBox5.Text == "0" ? "" : this.uiTextBox5.Text;
                        this.uiTextBox5.Text += buttonData.Text;
                    }
                    break;
                case 3:
                    if (buttonData.Text == "退格")
                    {
                        if (this.uiTextBox4.Text.Length > 0)
                        {
                            this.uiTextBox4.Text = this.uiTextBox4.Text.Substring(0, this.uiTextBox4.Text.Length - 1);
                        }
                        else
                        {
                            this.uiTextBox4.Text = "0";
                        }
                    }
                    else if (buttonData.Text == "清零")
                    {
                        this.uiTextBox4.Text = "0";
                    }
                    else
                    {
                        this.uiTextBox4.Text = this.uiTextBox4.Text == "0" ? "" : this.uiTextBox4.Text;
                        this.uiTextBox4.Text += buttonData.Text;
                    }
                    break;
                case 4:
                    if (buttonData.Text == "退格")
                    {
                        if (this.uiTextBox3.Text.Length > 0)
                        {
                            this.uiTextBox3.Text = this.uiTextBox3.Text.Substring(0, this.uiTextBox3.Text.Length - 1);
                        }
                        else
                        {
                            this.uiTextBox3.Text = "0";
                        }
                    }
                    else if (buttonData.Text == "清零")
                    {
                        this.uiTextBox3.Text = "0";
                    }
                    else
                    {
                        this.uiTextBox3.Text = this.uiTextBox3.Text == "0" ? "" : this.uiTextBox3.Text;
                        this.uiTextBox3.Text += buttonData.Text;
                    }
                    break;
            };
        }
        #endregion
        //关闭页面
        private void uiButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
